package com.songcha.module_bookdetail.ui.activity.catalog

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.songcha.library_base.interfaces.IApiResult
import com.songcha.library_base.mvvm.base.BaseViewModel
import com.songcha.library_business.bean.book.BookChapterCatalogBean
import com.songcha.library_business.bean.book.BookDetailNetBean
import com.songcha.library_business.proxy.database.BookDownloadDaoManager
import com.songcha.library_business.proxy.database.BookShelfDaoManager
import com.songcha.library_business.proxy.userinfo.UserInfoManager
import com.songcha.library_network.bean.BaseBean

class BookCatalogViewModel(app: Application):BaseViewModel<BookCatalogRepository>(app) {
    val chapterList= MutableLiveData<MutableList<BookChapterCatalogBean.DataBean>>(mutableListOf())
    //var bookId:String=""
    var bookDetail:BookDetailNetBean.DataBean?=null
    var isReverse=false
    var isInBookShelf=MutableLiveData<Boolean>()
    var isBookAllDownload=MutableLiveData<Boolean>()

    fun getBookChapterCatalog(){
        handleApiDataObserver(repository!!.getBookChapterCatalog(bookDetail!!.bookId),object :
            IApiResult<BookChapterCatalogBean> {
            override fun onSuccess(bean: BookChapterCatalogBean) {
                chapterList.value=bean.data
            }

            override fun onError(e: Throwable) {

            }

        },true)
    }

    fun addBookToShelf(){
        if(bookDetail==null) return
        if(BookShelfDaoManager.addToBookShelf(bookDetail!!.bookId.toString())){
            isInBookShelf.value=true
        }
        if(UserInfoManager.checkLogin()){
            handleApiDataObserver(repository!!.addToBookShelf(bookDetail!!.bookId),object :IApiResult<BaseBean>{
                override fun onSuccess(bean: BaseBean) {

                }
            })
        }
    }

    fun getIsBookInShelf(){
        if(bookDetail==null) return
        isInBookShelf.value=BookShelfDaoManager.isInBookShelf(bookDetail!!.bookId.toString())
    }

    fun getHasBookAllDownload(){
        if(bookDetail==null || chapterList.value==null) return
        if(chapterList.value!!.size==0) return
        isBookAllDownload.value=BookDownloadDaoManager.hasBookAllDownload(bookDetail!!.bookId.toString(),chapterList.value!!.size)
    }

}